package com.gmail.slang9393.library.utils;
import java.util.Vector;
import javax.swing.DefaultListModel;
import com.gmail.slang9393.library.bo.Book;
 
@SuppressWarnings("all")
public class BooksSearchUtils {
	
	public static interface ISearchCriteria {
		String getSearchValue(Book book);
	}

	private static ISearchCriteria authorSearchCriteria = new ISearchCriteria() {
		
		@Override
		public String getSearchValue(Book book) {
			return book.getAuthors();
		}
	};
	
	private static ISearchCriteria bookSearchCriteria = new ISearchCriteria() {
		
		@Override
		public String getSearchValue(Book book) {
			return book.getName();
		}
	};
	
	private static ISearchCriteria publisherSearchCriteria = new ISearchCriteria() {
		
		@Override
		public String getSearchValue(Book book) {
			return book.getPublisher();
		}
	};
	
	public static DefaultListModel search(Vector<Book> books, String value, ISearchCriteria searchCriteria) { 
    	DefaultListModel rez = new DefaultListModel();
    	for (Book book : books) {
    		String buf = searchCriteria.getSearchValue(book);
        	if (buf.toUpperCase().contains(value.toUpperCase())) {
                rez.addElement(book);
            }
		}
        return rez;
    }
    
    public static DefaultListModel searchByAuthor(Vector books, String author) { 
		return search(books, author, authorSearchCriteria);
    }
 
    public static DefaultListModel searchByBookName(Vector books, String bookName) {
		return search(books, bookName, bookSearchCriteria);
    }

    public static DefaultListModel searchByPublisher(Vector books, String bookPublisher) {
		return search(books, bookPublisher, publisherSearchCriteria);
    }

}
